﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Condimar.Entities;
using Condimar.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Condimar.UI
{
    public partial class FrmRegistrarProveedor : Form
    {
        [Dependency]
        public IProveedorBL provBL { get; set; }

        public FrmRegistrarProveedor()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool b;

        public void NuevoProveedor()
        {
            b = true;

            proveedorBindingSource.AddNew();
        }

        public Proveedor _proveedor { get; set; }

        public void EditarProveedor()
        {
            b = false;

            proveedorBindingSource.DataSource = _proveedor;
        }

        private void FrmRegistrarProveedor_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var proveedor = proveedorBindingSource.Current as Proveedor;

            if ((rucTextBox.Text).Length != 0 && (razonSocialTextBox.Text).Length != 0 && (direccionTextBox.Text).Length != 0)
            {
                if ((rucTextBox.Text).Length == 11)
                {
                    if (proveedor != null)
                    {
                        if (b == true)
                        {
                            provBL.AddProveedor(proveedor);
                            MessageBox.Show("Registro Correcto", "Registro",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                        if (b == false)
                        {
                            provBL.UpdateProveedor(proveedor);
                            MessageBox.Show("Modificación Correcta", "Registro",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                    }
                }
                else
                    MessageBox.Show("Longuitud de Ruc incorrecto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                MessageBox.Show("Debe completar todos los campos", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void rucTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
